//
//  CLLiveCell.m
//  chanlin
//
//  Created by Ryan on 15/6/14.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLLiveCell.h"
#import "UIView+Utils.h"
#import "UIImageView+WebCache.h"
#import "BlockUI.h"
#import "CLChatViewController.h"
#import "AQRImageZoomView.h"
#import "IFMoviePlayerController.h"

@interface CLLiveCell ()

@property (nonatomic,strong)IFMoviePlayerController *pc;

@property (nonatomic,strong)NSString *videoUrl;

@end

@implementation CLLiveCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        [self uiInit];
    }
    return self;
}

- (void)dealloc{
    if(self.pc){
        [[CLGloble sharedInstance] clearMovie];
    }
}

- (void)uiInit{
    self.backgroundColor=self.contentView.backgroundColor=RGB(246, 246, 246);
    self.icon=[[UIImageView alloc] init];
    self.icon.frame=CGRectMake(10.f, 16.f, 36.f, 36.f);
    self.icon.clipsToBounds=YES;
    self.icon.layer.cornerRadius=18.f;
    [self.contentView addSubview:self.icon];
    
    self.baseView=[[UIView alloc] init];
    self.baseView.frame=self.contentView.bounds;
    self.baseView.top=12.f;
    self.baseView.left=64.f;
    self.baseView.width=GlobleWidth-64.f-10.f;
    self.baseView.height-=12.f;
    self.baseView.autoresizingMask=UIViewAutoresizingFlexibleHeight;
    self.baseView.backgroundColor=[UIColor whiteColor];
    
    UIImageView *cor=[[UIImageView alloc] initWithFrame:CGRectMake(self.baseView.left-7, 30, 7, 13)];
    cor.image=[UIImage imageNamed:@"liveRoom_anLeft.png"];
    [self.contentView addSubview:cor];
    
    self.label1=[UILabel new];
    self.label1.font=[UIFont systemFontOfSize:11.f];
    self.label1.textAlignment=NSTextAlignmentLeft;
    self.label1.textColor=RGB(0x75, 0x79, 0x85);
    self.label1.frame=CGRectMake(CLLiveCell_mesage_padding, 12.f, 45.f, self.label1.font.lineHeight);
    [self.baseView addSubview:self.label1];
    
    self.label2=[UILabel new];
    self.label2.font=self.label1.font;
    self.label2.textAlignment=NSTextAlignmentLeft;
    self.label2.textColor=self.label1.textColor;
    self.label2.frame=CGRectMake(self.label1.right+8.f, 12.f, 45.f, self.label2.font.lineHeight);
    [self.baseView addSubview:self.label2];
    
    self.label3=[UILabel new];
    self.label3.font=[UIFont systemFontOfSize:10.f];
    self.label3.textColor=self.label1.textColor;
    self.label3.frame=CGRectMake(self.baseView.width-45.f-CLLiveCell_mesage_padding, 12.f, 45.f, self.label1.font.lineHeight);
    self.label3.textAlignment=NSTextAlignmentRight;
    [self.baseView addSubview:self.label3];
    [self.contentView addSubview:self.baseView];
    
    
    self.titleLabel=[UILabel new];
    self.titleLabel.font=[UIFont systemFontOfSize:17];
    self.titleLabel.numberOfLines=0;
    self.titleLabel.textColor=RGB(67, 70, 78);
    [self.baseView addSubview:self.titleLabel];
    
    self.img=[[UIImageView alloc] init];
    self.img.frame=CGRectMake(0, 0, 229, 172);
    self.img.userInteractionEnabled=YES;
    self.img.contentMode=UIViewContentModeScaleAspectFit;
    self.img.clipsToBounds=YES;
    [self.baseView  addSubview:self.img];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPic)];
    [self.img addGestureRecognizer:tap];
    
    
    
    self.contengLabel=[UILabel new];
    self.contengLabel.font=[UIFont systemFontOfSize:CLLiveCell_contentFont];
    self.contengLabel.numberOfLines=0;
    self.contengLabel.textColor=RGB(0x56, 0x56, 0x5e);
    [self.baseView addSubview:self.contengLabel];
    
    
  
    __weak typeof(self) me=self;
    self.cbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.cbtn setImage:[UIImage imageNamed:@"live_copy.png"] forState:UIControlStateNormal];
    self.cbtn.frame=CGRectMake(self.baseView.width-CLLiveCell_mesage_padding-16, 0, 16, 16);
    [self.baseView addSubview:self.cbtn];
    [self.cbtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.chat thecopy:me.path];
    }];
    
    self.replay=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.replay setImage:[UIImage imageNamed:@"live_reply.png"] forState:UIControlStateNormal];
    self.replay.frame=CGRectMake(self.cbtn.left-CLLiveCell_mesage_padding-16, 0, 16, 16);
    [self.baseView addSubview:self.replay];
    [self.replay handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.chat reply:me.path];
    }];
    
    self.support=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.support setImage:[UIImage imageNamed:@"live_support.png"] forState:UIControlStateNormal];
    self.support.frame=CGRectMake(self.replay.left-CLLiveCell_mesage_padding-16, 0, 16, 16);
    [self.baseView addSubview:self.support];
    
    [self.support handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        me.support.enabled=NO;
        [me.chat support:me.path];
        
        [me performSelector:@selector(enablesupport) withObject:nil afterDelay:0.5];
    }];
    
    self.comments=[UILabel new];
    self.comments.font = [UIFont fontWithName:@"Avenir-Book" size:10];
    self.comments.textColor=RGB(0x5b, 0x85, 0x9e);
    self.comments.textAlignment=NSTextAlignmentRight;
    self.comments.frame=CGRectMake(self.support.left-7-90, 0, 90, 20);
    [self.baseView addSubview:self.comments];
    
    self.replayView=[UIView new];
    self.replayView.frame=CGRectMake(CLLiveCell_reply_marge, 0, self.baseView.width-(2*CLLiveCell_reply_marge), 0);
    self.replayView.backgroundColor=RGB(0xf3, 0xf3, 0xf3);
    [self.baseView addSubview:self.replayView];
    
    
    
    self.label5=[UILabel new];
    self.label5.font=[UIFont systemFontOfSize:11.f];
    self.label5.textAlignment=NSTextAlignmentLeft;
    self.label5.textColor=RGB(0x75, 0x79, 0x85);
    self.label5.frame=CGRectMake(CLLiveCell_mesage_padding, 12.f, 45.f, self.label1.font.lineHeight);
    [self.replayView addSubview:self.label5];
    
    self.label6=[UILabel new];
    self.label6.font=self.label5.font;
    self.label6.textAlignment=NSTextAlignmentLeft;
    self.label6.textColor=self.label5.textColor;
    self.label6.frame=CGRectMake(self.label5.right+8.f, 12.f, 45.f, self.label2.font.lineHeight);
    [self.replayView addSubview:self.label6];
    
    self.label7=[UILabel new];
    self.label7.font=[UIFont systemFontOfSize:13];
    self.label7.numberOfLines=0;
    self.label7.textColor=RGB(0x56, 0x56, 0x5e);
    self.label7.frame=CGRectMake(CLLiveCell_mesage_padding, self.label5.bottom+12.f, 45.f, 0);

    [self.replayView addSubview:self.label7];
    [self.contentView addSubview:self.baseView];

    
}

- (void)enablesupport{
    self.support.enabled=YES;
}

- (void)showPic{
    if(_isStrNotNull(self.videoUrl))return;
    if(!self.img.image) return;
    
    CGPoint point=CGPointZero;
    CGRect aframe;
  
        aframe=self.img.frame;
        point=self.img.frame.origin;
        point=[self convertPoint:point toView:[self k_currentController].view];
   
    
    self.bigImgView=[[UIView alloc] initWithFrame:[self k_currentController].view.bounds];
    self.bigImgView.backgroundColor=[UIColor blackColor];
    
    
    AQRImageZoomView *zoom=[[AQRImageZoomView alloc] initWithFrame:self.bigImgView.bounds];
   // [zoom setImgUIViewContentMode:UIViewContentModeCenter];
    zoom.fromRect=CGRectMake(point.x, point.y, aframe.size.width, aframe.size.height);
    [self.bigImgView  addSubview:zoom];
    [[self k_currentController].view addSubview:self.bigImgView];
    [zoom setImageUrl:self.imgString];
    [zoom tapSelect:@selector(cancleImg) target:self];
    UIButton *download=[UIButton buttonWithType:UIButtonTypeCustom];
    download.frame=CGRectMake(GlobleWidth-50.f, GlobleHeight-50.f, 50.f, 50.f);
    [download setImage:[UIImage imageNamed:@"slider_font.png"] forState:UIControlStateNormal];
    download.contentEdgeInsets=UIEdgeInsetsMake(26, 26, 0, 0);
    [self.bigImgView addSubview:download];
    __weak AQRImageZoomView *wz=zoom;
    [download handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        UIImage *img=[wz image];
        if(img)
            UIImageWriteToSavedPhotosAlbum(img, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);
        
    }];
}

- (void)cancleImg{
    [UIView animateWithDuration:0.2 animations:^{
        self.bigImgView.alpha=0;
    } completion:^(BOOL finished) {
        [self.bigImgView removeFromSuperview];
        self.bigImgView=nil;
    }];
}


#pragma mark - download delegate
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString * message = [error description];
    if ([message length]==0) {
        [[CLGloble sharedInstance] showTieSuccessMessage:@"提示" subMessage:@"保存成功"];
    }
    else
    {
        message = @"请开启相册访问权限";
        [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:message];
    }
}

- (void)load:(CLLive *)live{
    self.support.hidden=NO;
    self.support.enabled=YES;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:live.usrImg]];
    if(live.room==1){
        self.label1.text=@"主持人";
    }else if(live.room==2){
        self.label1.text=@"游客";
    }
    self.label2.text=live.nickName;
    
    float y=self.label1.bottom;
    CGFloat width=GlobleWidth-2*CLLiveCell_mesage_padding-CLLiveCell_mesage_left-CLLiveCell_mesage_right;
    if(_isStrNotNull(live.title)){
        self.titleLabel.text=live.title;
        self.titleLabel.frame=CGRectMake(CLLiveCell_mesage_padding, y+12, width, live.titleHeight);
        y=self.titleLabel.bottom;
        self.titleLabel.hidden=NO;
    }else{
        self.titleLabel.hidden=YES;
    }
    
    
    if(_isStrNotNull(live.img)){
        self.imgString=live.img;
        [self.img sd_setImageWithURL:[NSURL URLWithString:live.img]];
        self.img.frame=CGRectMake(CLLiveCell_mesage_padding, y+12, width, 172);
        y=self.img.bottom;
        self.img.hidden=NO;
        self.img.userInteractionEnabled=YES;
        
        if(_isStrNotNull(live.videoUrl)){
            self.videoUrl=live.videoUrl;
            if(self.playButton==nil){
            self.playButton=[UIButton buttonWithType:UIButtonTypeCustom];
            self.playButton.frame=CGRectMake(0, 0, 48, 48);
            [self.playButton setImage:[UIImage imageNamed:@"doc_playImage.png"] forState:UIControlStateNormal];
            [self.playButton setCenter:CGPointMake(self.img.width/2.f, self.img.height/2.f)];
            [self.img addSubview:self.playButton];
            
            __weak typeof(self) me=self;
          
            [self.playButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
                if (me.videoUrl&& [me.videoUrl length] > 4){
                    [[CLGloble sharedInstance] clearMovie];
                    CGRect frame=me.img.bounds;
                    IFMoviePlayerController *moviePlayer = [[IFMoviePlayerController alloc] initWithContentURL:nil WithType:IFVideo_Type_Document];
                    //self.moviePlayer.delegate = self;
                    //self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
                    
                    [moviePlayer.view setFrame:frame];
                    
                    
                    [me.img addSubview:moviePlayer.view];
                    
                    [moviePlayer setContentURL:[NSURL URLWithString:me.videoUrl]];
                    [moviePlayer prepareToPlay];
                    [moviePlayer play];
                    me.pc=moviePlayer;
                    [CLGloble sharedInstance].singleMovie=moviePlayer;
                    
                    
                }
            }];
            }else{
                self.playButton.hidden=NO;
            }
            
           

        }else{
            self.videoUrl=nil;
            self.playButton.hidden=YES;
            
        }
        
    }else{
        self.img.hidden=YES;
        self.imgString=nil;
        self.img.userInteractionEnabled=NO;
        [self.img sd_setImageWithURL:nil];
        
       
    }
    

    
    y+=3;
    
    if(_isStrNotNull(live.content)){
        self.contengLabel.frame=CGRectMake(CLLiveCell_mesage_padding, y, width, live.contentHeight);
        self.contengLabel.text=live.content;
        self.contengLabel.hidden=NO;
        y=self.contengLabel.bottom;
    }else{
        self.contengLabel.hidden=YES;
    }
    if(live.time>0){
        self.label3.text=[[CLGloble sharedInstance].formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(live.time/1000.f)]];
    }else{
        self.label3.text=@"";
    }
    
    if(_isStrNotNull(live.replyname)){
        self.replayView.top=y;
        self.replayView.height=24+live.replycHeight+13+12;
        
        if([@"1" isEqualToString:live.replyroom]){
            self.label5.text=@"主持人";
        }else if([@"2" isEqualToString:live.replyroom]){
            self.label5.text=@"游客";
        }
        
        self.label6.text=live.replyname;
        if(_isStrNotNull(live.replycontent)){
            CGFloat width2=width-4*CLLiveCell_reply_marge;
            self.label7.text=live.replycontent;
            self.label7.hidden=NO;
            self.label7.frame=CGRectMake(CLLiveCell_reply_marge, self.label5.bottom+12, width2, live.replycHeight);
        }else{
            self.label7.text=@"";
            self.label7.hidden=YES;
        }
        
        
        y=self.replayView.bottom;
        self.replayView.hidden=NO;
    }else{
        self.replayView.hidden=YES;
        
    }
    
    self.cbtn.top=self.support.top=self.replay.top=self.comments.top=y;
 
    if([[CLGloble sharedInstance].supports containsObject:[NSString stringWithFormat:@"%@%@",live.liveId,live.cmtId]]){
        [self.support setImage:[UIImage imageNamed:@"live_support2.png"] forState:UIControlStateNormal];
    }else{
        [self.support setImage:[UIImage imageNamed:@"live_support.png"] forState:UIControlStateNormal];
      
    }
    
    if(live.support==0){
        self.comments.hidden=YES;
    }else{
        self.comments.hidden=NO;
        self.comments.text=[NSString stringWithFormat:@"%d",live.support];
    }
    
    
   
    
    
    
    
}

@end
